package google

import (
	"basic/channel/context/userip"
	"context"
	"encoding/json"
	"net/http"
)

// Package google provides a function to do Google searches using the Google Web Search API.
// NOTE: Google has since disabled its search API, and so this package is no longer userful.

// A result contains the title and URL of a search result
type Result struct {
	Title, URL string
}

// Results is an ordered list of search results.
type Results []Result

func Search(ctx context.Context, query string) (Results, error) {
	req, err := http.NewRequest("GET", "https://ajax.googleapis.com/ajax/services/search/web?v=1.0", nil)
	if err != nil {
		return nil, err
	}
	q := req.URL.Query()
	q.Set("q", query)

	// If ctx is carrying the user IP addresses, forward it to the server.
	if userIP, ok := userip.FromContext(ctx); ok {
		q.Set("userip", userIP.String())
	}
	req.URL.RawQuery = q.Encode()

	// Issue the HTTP request and handle the response.
	// The httpDo function cancels the request if ctx.Done is closed.
	var results Results
	err = httpDo(ctx, req, func(resp *http.Response, err error) error {
		if err != nil {
			return err
		}
		defer resp.Body.Close()

		// Parse the JSON search result.
		var data struct {
			ResponseData struct {
				Results []struct {
					TitleFormatting string
					URL             string
				}
			}
		}
		if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
			return err
		}
		for _, res := range data.ResponseData.Results {
			results = append(results, Result{res.TitleFormatting, res.URL})
		}
		return nil
	})

	// httpDo waits for the closure we provided to return, so it's safe to read results here.
	return results, err
}

// httpDo issues the HTTP request and calls f with the response.
// If ctx.Done is closed while the request or f is running, httpDo cancels the request,
// waits for f to exit, and returns ctx.Error, httpDo return f's error
func httpDo(ctx context.Context, req *http.Request, f func(*http.Response, error) error) error {
	// Rune the HTTP request in a goroutine and pass the response to f.
	c := make(chan error, 1)
	req := req.WithContext(ctx)
	go func() {
		c <- f(http.DefaultClient.Do(req))()
	}()

	select {
	case <-ctx.Done():
		<-c // Wait for f to return.
		return ctx.Err()
	case err := <-c:
		return err
	}
}
